#include "NContactListener.h"
#include "NeroEngine\NEntity.h"

NContactListener::NContactListener(void)
{
}

NContactListener::~NContactListener(void)
{
}

void NContactListener::PreSolve(b2Contact* contact, const b2Manifold* oldManifold)
{
	b2ContactListener::PreSolve(contact, oldManifold);
}

void NContactListener::PostSolve(b2Contact* contact, const b2ContactImpulse* impulse)
{
	b2ContactListener::PostSolve(contact, impulse);
}

void NContactListener::BeginContact(b2Contact* contact)
{
	Game::NEntity* object = NULL;
	CollisionEvent event;

	void* bodyA_UserData = contact->GetFixtureA()->GetBody()->GetUserData();
	void* bodyB_UserData = contact->GetFixtureB()->GetBody()->GetUserData();

	object = (Game::NEntity*) bodyA_UserData ;
	if(object != NULL)
	{
		event.Self = (void*) bodyA_UserData;
		event.Other = (void*) bodyB_UserData;

		(object->*(object->BeginCollision))((void*) &event);
	}

	object = (Game::NEntity*) bodyB_UserData;
	if(object != NULL)
	{
		event.Self = (void*) bodyB_UserData;
		event.Other = (void*) bodyA_UserData;

		(object->*(object->BeginCollision))((void*) &event);
	}
}

void NContactListener::EndContact(b2Contact* contact)
{
	Game::NEntity* object = NULL;

	void* bodyA_UserData = contact->GetFixtureA()->GetBody()->GetUserData();
	object = (Game::NEntity*) bodyA_UserData;
	if(object != NULL)
	{
		(object->*(object->EndCollision))((void*) contact);
	}

	void* bodyB_UserData = contact->GetFixtureB()->GetBody()->GetUserData();
	object = (Game::NEntity*) bodyB_UserData ;
	if(object != NULL)
	{
		(object->*(object->EndCollision))((void*) contact);
	}
}
